package commonAdmin.domain;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author Jack
 *
 * @param <T>
 */
public class Page<T> {

	/** PAGE_SIZE=10 **/
	public static int PAGE_SIZE = 10;
	/** FIRST_PAGE_NUM = 1**/
	public static int FIRST_PAGE_NUM = 1;

	private Integer pageNum = 1;
	private Integer pageSize = PAGE_SIZE;
	private Integer totalItem = 0;
	private Integer totalPage = 0;
	private List<T> items = new ArrayList<T>();

	private Integer startPageNum;//页面上的起始页码
	private Integer endPageNum;

	public void setStartPageNum(Integer startPageNum) {
		this.startPageNum = startPageNum;
	}

	public void setEndPageNum(Integer endPageNum) {
		this.endPageNum = endPageNum;
	}

	public Integer getStartPageNum() {
		if (totalPage <= 10) {
			startPageNum = 1;
			endPageNum = totalPage;
		} else {
			int start = pageNum - 5;
			int end = pageNum + 4;
			if (start > 0) {
				if (end >= totalPage) {
					endPageNum = totalPage;
					startPageNum = totalPage - 9;
				} else {
					startPageNum = start;
					endPageNum = end;
				}
			} else {
				startPageNum = 1;
				endPageNum = 10;
			}
		}

		return startPageNum;
	}

	public Integer getEndPageNum() {
		return endPageNum;
	}

	public Page() {

	}

	public Page(List<T> items, Integer totalItem) {
		this(items, FIRST_PAGE_NUM, PAGE_SIZE, totalItem);
	}

	public Page(List<T> items, Integer pageNum, Integer totalItem) {
		this(items, pageNum, PAGE_SIZE, totalItem);
	}

	public Page(List<T> items, Integer pageNum, Integer pageSize, Integer totalItem) {
		if (items == null) {
			this.items = new ArrayList<T>(0);
		}
		this.totalItem = totalItem == null ? 1 : totalItem;
		if (pageSize == null)
			this.pageSize = PAGE_SIZE;
		if (pageNum != null && getTotalPage() > 1) {
			pageNum = Math.max(pageNum, FIRST_PAGE_NUM);
			pageNum = Math.min(pageNum, getTotalPage());
		} else {
			pageNum = FIRST_PAGE_NUM;
		}
		this.pageNum = pageNum;

	}

	public boolean hasContent() {
		return items != null && !items.isEmpty();
	}

	public boolean hasPrevious() {
		return pageNum > FIRST_PAGE_NUM && pageNum <= getTotalPage();
	};

	public boolean hasNext() {
		return pageNum < getTotalPage();
	};

	public Integer getPageNum() {
		return pageNum;
	}

	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public Integer getTotalItem() {
		//totalItem = items.size();
		return totalItem;
	}

	public void setTotalItem(Integer totalItem) {
		this.totalItem = totalItem;
	}

	public List<T> getItems() {
		return items;
	}

	public void setItems(List<T> items) {
		this.items = items;
	}

	public void setTotalPage(Integer totalPage) {
		this.totalPage = totalPage;
	}

	public Integer getTotalPage() {
		totalPage = getTotalItem() > 0 ? (getTotalItem() - 1) / pageSize + 1 : 0;
		return totalPage;
	}

}
